//app.js
$(function(){

  /* ===========预定义变量========== */
  var $variable = $('input[name=variable]'),
    
    $source = $('#jssource'),
    
    $report = $('.lint_report'),
    
    base = [
      'bitwise',
      'browser',
      'cap',
      'continue',
      'css',
      'debug',
      'devel',
      'eqeq',
      'es5',
      'evil',
      'forin',
      'fragment',
      'indent',
      'maxerr',
      'maxlen',
      'newcap',
      'node',
      'nomen',
      'on',
      'passfail',
      'plusplus',
      'regexp',
      'rhino',
      'sloppy',
      'stupid',
      'sub',
      'undef',
      'unparam',
      'vars',
      'white',
      'windows'
    ],
    
    only_directives = ['anon', 'properties'],   //directive独有选项
    
    only_options = ['adsafe', 'predef', 'safe'],  //配置独有选项
    
    options = base.concat(only_options),     //配置项
    
    directives = base.concat(only_directives),   //指令
    
    default_options = {    //默认配置
      'adsafe': false,
      'bitwise': true,
      'browser': true,
      'cap': true,
      'continue': true,
      'css': true,
      'debug': true,
      'devel': true,
      'eqeq': true,
      'es5': true,
      'evil': true,
      'forin': true,
      'fragment': true,
      'indent': 4,
      'maxerr': 50,
      'maxlen': 80,
      'newcap': true,
      'node': false,
      'nomen': true,
      'on': true,
      'passfail': false,
      'plusplus': true,
      // 'predef': '',
      'regexp': true,
      'rhino': false,
      'safe': true,
      'sloppy': true,
      'stupid': true,
      'sub': true,
      'undef': true,
      'unparam': true,
      'vars': true,
      'white': true,
      'windows': true
    };

  


  /* ==========预定义方法======== */
  /* 获取用户指定的预定义变量 */
  function get_predef(){
    var var_str = $variable.val().trim().split(' '), variables = [];
    for(var i = 0 ; i < var_str.length; i++)
      if(var_str[i] !== '' && variables.indexOf(var_str[i]) == -1) variables.push(var_str[i]);
    return variables;
  }

  /* 获取配置项 */
  function get_options(){
    var options = {}, $checkboxes = $('input[type=checkbox]');
    $($checkboxes).each(function(i, j){
      options[j.name] = j.checked;
    });
    return options;
  }

  /* 设置配置项为默认选项 */
  function set_default_options(){
    for(var i in default_options){
      var dom = $('input[name=' + i + ']').get(0), val = default_options[i];
      if(dom) dom[typeof(val) === 'boolean' ? 'checked' : 'value'] = val;
    }
  }

  /* ===========事件绑定========== */
  /* variable清空按钮点击事件 */
  $('.predef_variable .empty').click(function(e){
    $variable.val('');
  });

  /* js代码清空按钮点击事件 */
  $('.js_source .empty').click(function(e){
    $source.val('');
    $report.html('');
  });

  /* 重置配置项 */
  $('.config .reset').click(function(){
    set_default_options();
  });

  /* JSLINT按钮点击事件 */
  $('.js_source .button').click(function(e){
    
    var source_code = $source.val().trim(), variables = get_predef(),
    
    options = get_options(), report_msg;
    
    options.predef = variables;
    options.indent = parseInt($('input[name=indent]').val(), 10);
    options.maxlen = parseInt($('input[name=maxlen]').val(), 10);
    options.maxerr = parseInt($('input[name=maxerr]').val(), 10);
    
    var lintResult = JSLINT(source_code, options);
    
    if(source_code === ''){
      report_msg = "<div class='ac mt100'>You didn't give me some code, Please input some and retry!<div>";
    }else if(lintResult){  //代码格式良好
      report_msg = "<div class='ac mt100'>Congratulations !!! &nbsp; Your code is pretty beauty.<div>";
    }else{   //代码格式报告
      var errors = JSLINT.errors,
        result = JSLINT.data(),
        // report = JSLINT.report(result),
        error_report = JSLINT.error_report(result),
        property_report = JSLINT.properties_report(JSLINT.property),
        tree = JSLINT.tree;

      report_msg = error_report;
    }
    
    $report.html(report_msg);
  });



  

  /* ===========初始化操作========== */
  set_default_options();

  
});

